import os
import nibabel as nib
import numpy as np
import cv2

src_dir = "/data/suziren25/suziren/nnUNet/nnUNet_raw/abdominal_US/new_images"
dst_dir = "/data/suziren25/suziren/nnUNet/nnUNet_raw/Dataset506_KidneyUS/imagesTs"

os.makedirs(dst_dir, exist_ok=True)

for fname in os.listdir(src_dir):
    if fname.lower().endswith((".jpg", ".png")):
        img_path = os.path.join(src_dir, fname)
        img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
        img = np.expand_dims(img, axis=-1)  # shape: (H, W, 1)
        nii_img = nib.Nifti1Image(img, np.eye(4))
        base = os.path.splitext(fname)[0]
        out_path = os.path.join(dst_dir, f"{base}_0000.nii.gz")
        nib.save(nii_img, out_path)
        print(f"✅ saved: {out_path}")